--[[

	GModRP - Real Garry's Mod Roleplay.
			By Slob187.

]]

require( "glon" )

cDN = "rpcompany"

function GMRP.companyLoc( p )

	if ( p:GetPData( cDN ) == "" ) then return end

	return "GModRP/companies/"..p:GetPData( cDN )..".txt"
	
end


function GMRP.getCompanyTable( p )

	if ( p:GetPData( cDN ) == "" ) then return end
	
	local cDat = GMRP.companyLoc( p )

	if file.Exists( cDat ) then
	
		return glon.decode( file.Read( cDat ) )
		
	end

end	


function GMRP.getCompanyMembers( c )

	local cL = "GModRP/companies/"..c..".txt"	
	local cM = {}
	
	if file.Exists( cL ) then
	
		local cDat = glon.decode( file.Read( cL ) )
		
		for k, v in pairs( cDat[4] ) do
		
			for k2, v2 in pairs( player.GetAll() ) do
			
				if ( tostring( v ) == v2:UniqueID() ) then
				
					table.insert( cM, v2 )
					
				end

			end
			
		end
		
	end
	
	return cM
	
end
		

function GMRP.SendCompanyData( c, p )

	if ( p:GetPData( cDN ) == "" ) then return end
	
	local companyLoc = "GModRP/companies/"..c..".txt"

	if ( file.Exists( companyLoc ) ) then
	
		local fTable = glon.decode( file.Read( companyLoc ) )
		
		p:SendLua( "companyData = {}" )	
		
		for k, v in pairs( fTable ) do

			if ( type( v ) == "string" ) then
			
				p:SendLua( "companyData["..tonumber( k ).."] = '"..v.."'" )
				
			end
			
		end
		
		p:SendLua( "companyData[4] = {}" )
		
		for k2, v2 in pairs( GMRP.getCompanyMembers( c ) ) do
		
			p:SendLua( "companyData[4]["..tonumber( k2 ).."] = "..v2:EntIndex() )
			
		end			
		
	end
	
end


function GMRP.sendCompanyDataToMembers( c )

	local cL = "GModRP/companies/"..c..".txt"	
	
	if file.Exists( cL ) then
	
		for k, v in pairs( GMRP.getCompanyMembers( c ) ) do
		
			GMRP.SendCompanyData( c, v )

		end
	
	end
	
end
		
		
function GMRP.createCompany( p, c, a )

	if ( p:GetPData( cDN ) != "" ) then return end

	if ( a == nil ) then return end
	
	local cN = string.lower( ef( string.Implode( "_", a ) ) )	
	
	if ( string.len( cN ) <= 3 ) then
	
		p:RedNotify( "Company name is too short!" )
		return
		
	elseif ( string.len( cN ) > 15 ) then

		p:RedNotify( "Company name is too long!" )
		return	
	
	end
	
	if ( !p:HasMoney( Configuration["companyprice"] ) ) then return end
	
	local pCL = "GModRP/companies/"..cN..".txt"	
	
	if ( !file.Exists( pCL ) ) then
	
		p:SetPData( cDN, cN )
		file.Write( pCL, glon.encode( { cN, "MingeBag company.", "Sell melons.", { p:UniqueID() } } ) )
		GMRP.SendCompanyData( cN, p )
		
		p:TakeMoney( Configuration["companyprice"] )
		
		p:GreenNotify( "Company created!" )
	
	else
	
		p:RedNotify( "This company already exists." )
		
	end
	
end	

GMRP.command( "company_create", GMRP.createCompany )


function GMRP.setCompanyName( p, c, a )

	if ( p:GetPData( cDN ) == "" ) then return end

	if ( !GMRP.UseLimit( p ) ) then return end

	if ( a == nil ) then return end

	local cD = string.Implode( " ", a )
	local pCL = GMRP.companyLoc( p )

	if file.Exists( pCL ) then
	
		local cTable = GMRP.getCompanyTable( p )
		cTable[1] = cD
		
		file.Write( pCL, glon.encode( cTable ) )
		
		GMRP.sendCompanyDataToMembers( p:GetPData( cDN ) )
	
	end
	
end

GMRP.command( "company_setname", GMRP.setCompanyName )


function GMRP.setCompanyInfo( p, c, a )

	if ( p:GetPData( cDN ) == "" ) then return end

	if ( !GMRP.UseLimit( p ) ) then return end

	if ( a == nil ) then return end

	local cD = string.Implode( " ", a )
	local pCL = GMRP.companyLoc( p )

	if file.Exists( pCL ) then
	
		local cTable = GMRP.getCompanyTable( p )
		
		cTable[2] = cD
		
		file.Write( pCL, glon.encode( cTable ) )	

		GMRP.sendCompanyDataToMembers( p:GetPData( cDN ) )		
		
	end
	
end

GMRP.command( "company_setinfo", GMRP.setCompanyInfo )


function GMRP.setCompanyAgenda( p, c, a )

	if ( p:GetPData( cDN ) == "" ) then return end

	if ( !GMRP.UseLimit( p ) ) then return end

	if ( a == nil ) then return end

	local cD = string.Implode( " ", a )
	local pCL = GMRP.companyLoc( p )

	if file.Exists( pCL ) then
	
		local cTable = GMRP.getCompanyTable( p )
		
		cTable[3] = cD
		
		file.Write( pCL, glon.encode( cTable ) )

		GMRP.sendCompanyDataToMembers( p:GetPData( cDN ) )		
		
	end
	
end

GMRP.command( "company_setagenda", GMRP.setCompanyAgenda )


function GMRP.addCompanyMember( p, c, a )

	if ( p:GetPData( cDN ) == "" ) then return end

	if ( !GMRP.UseLimit( p ) ) then return end

	if ( a == nil ) then return end

	local cD = string.Implode( " ", a )
	local pCL = GMRP.companyLoc( p )

	if file.Exists( pCL ) then
	
		local cTable = GMRP.getCompanyTable( p )
		
		for k, v in pairs( player.GetAll() ) do
		
			if ( !string.find( string.lower( v:Nick() ), string.lower( cD ) ) ) then
			
				p:RedNotify( "Player not found." )
				
				return
				
			elseif ( v:GetPData( cDN ) == "" ) then
			
				p:RedNotify( "This player is already in company." )
				
				return
				
			elseif ( table.HasValue( cTable[4], v:UniqueID() ) ) then
			
				p:RedNotify( "This player is already in your company." )
				
				return
			
			else
			
				table.insert( cTable[4], v:UniqueID() )
				
				file.Write( pCL, glon.encode( cTable ) )	
				GMRP.sendCompanyDataToMembers( p:GetPData( cDN ) )		

				p:GreenNotify( "Member added sucefully." )
				
			end	

		end	
		
	end
	
end

GMRP.command( "company_addmember", GMRP.addCompanyMember )


function GMRP.leaveCompany( p )

	if ( p:GetPData( cDN ) == "" ) then return end
	
	local pCL = GMRP.companyLoc( p )
	
	if ( file.Exists( pCL ) ) then

		local fTable = glon.decode( file.Read( pCL ) )
		
		if ( tostring( fTable[4][1] ) == p:UniqueID() ) then
		
			for k, v in pairs( GMRP.getCompanyMembers( p:GetPData( cDN ) ) ) do
			
				v:SendLua( "companyData = {}" )
				
			end			
		
			file.Delete( pCL )	
			p:SetPData( cDN, "" )

			
		else
		
			for k, v in pairs( fTable[4] ) do
			
				if ( tostring( v ) == p:UniqueID() ) then
				
					fTable[4][k] = nil

				end
				
			end
			
			file.Write( pCL, glon.encode( fTable ) )
			GMRP.sendCompanyDataToMembers( p:GetPData( cDN ) )
			p:SetPData( cDN, "" )			
			
		end
	
	end
	
end
	
GMRP.command( "company_leave", GMRP.leaveCompany )